package com.myapplication.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.myapplication.helper.PreferencesHelper1;
import com.myapplication.zhou.alldemo.R;
import com.myapplication.zhou.alldemo.SearchPageActivity;

/**
 * Created by zhoushikun on 2016/6/30.
 */
public class SortPageRightRecycleViewAdapter extends RecyclerView.Adapter<SortPageRightRecycleViewAdapter.MyViewHolder> {
    public Context context;
    public String[] text_array = null;
    public LayoutInflater layoutInflater;
    public String[] gcc_id = null;


    public SortPageRightRecycleViewAdapter(Context context, String[] text_array, String[] gcc_id) {
        this.context = context;
        this.text_array = text_array;
        this.gcc_id = gcc_id;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(R.layout.sort_page_right_recycle_view, parent, false);
        MyViewHolder myViewHolder = new MyViewHolder(view, listener);
        return myViewHolder;
    }

    private SortPageRightOnClickListener listener = new SortPageRightOnClickListener() {
        @Override
        public void sortPageOnClickListener(View v, String[] id, int position) {
            Intent intent = new Intent();
            intent.setClass(context, SearchPageActivity.class);
            Bundle bundle = new Bundle();
            setSh1(context,"main","tag","search");
            Log.i("gcc",gcc_id[position]+"ksksk"+text_array[position]);
            bundle.putString("keyword",gcc_id[position]);
            bundle.putString("keyname","gc_id");
            intent.putExtras(bundle);
            context.startActivity(intent);
        }
    };

    public void setSh1(Context context, String name, String key, String value){
        PreferencesHelper1.setSharedValue(context,name,key,value);
    }

    public String getSh1(Context context,String name,String key){
        return PreferencesHelper1.getSharedValue(context,name,key);
    }
    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.textView_title.setText(text_array[position].replace("\r\n",""));
        holder.bindView(text_array);
    }

    @Override
    public int getItemCount() {
        if (text_array == null) {
            return 0;
        } else
            return text_array.length;
    }

    public void update(String[] arr, String[] gcc_id_array) {
        this.text_array = arr;
        this.gcc_id = gcc_id_array;
        notifyDataSetChanged();

    }

    public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private LinearLayout layout;
        private TextView textView_title;
        private SortPageRightOnClickListener listener;

        public MyViewHolder(View itemView, SortPageRightOnClickListener listener) {
            super(itemView);
            this.listener = listener;
            layout = (LinearLayout) itemView.findViewById(R.id.layout);
            textView_title = (TextView) itemView.findViewById(R.id.textView_title);
            layout.setOnClickListener(this);
        }

        public void bindView(String[] id) {
            layout.setTag(id);
        }

        @Override
        public void onClick(View v) {
            if (listener != null) {
                listener.sortPageOnClickListener(v, (String[]) v.getTag(), getLayoutPosition());
            }
        }
    }

    public interface SortPageRightOnClickListener {
        void sortPageOnClickListener(View v, String[] id, int position);


    }
}
